package org.zjx.app.action.gather;

import org.zjx.analysis.analyze.flag.*;
import org.zjx.app.action.AbstractWindowAction;
import org.zjx.window.window.IWindow;

import java.awt.*;
import java.util.List;
import java.util.Map;

public class GatherFlagAction extends AbstractWindowAction {
    private final Map<String, Object> map;
    private  BaseFlagAnalysis baseFlagAnalysis;
    private final String cityName;

    public GatherFlagAction(IWindow targetWindow, String cityName, Map<String, Object> map) {
        super(targetWindow);
        this.map = map;
        this.cityName = cityName;
        analysis();
    }
    public void analysis(){
        switch (cityName){
            case "长安城":
                baseFlagAnalysis = new FlagsCACAnalysis(targetWindow);
                break;
            case "傲来国":
                baseFlagAnalysis = new FlagsALGAnalysis(targetWindow);
                break;
            case "长寿村":
                baseFlagAnalysis = new FlagsCSCAnalysis(targetWindow);
                break;
            case "朱紫国":
                baseFlagAnalysis = new FlagsZZGAnalysis(targetWindow);
                break;
            default:
                throw new IllegalArgumentException("合成旗城市未收录:"+cityName);
        }
    }

    @Override
    public void open() {

    }

    @Override
    public void close() {
        if (!baseFlagAnalysis.isOpened()) return;
        Rectangle closeButton = baseFlagAnalysis.getCloseButtonPoint();
        targetWindow.actions().clickIn(closeButton).delay(300);
        targetWindow.flushScreenshot();
        analysis();
        close();
    }

    @Override
    public void reSetWindow() {

    }

    @Override
    public void mouseAloofWindow(Rectangle rect) {
        if (baseFlagAnalysis.mouseInRect(rect)) {
            Point safePoint = baseFlagAnalysis.findLargestRemainingAreaCenter(rect);
            targetWindow.actions().gameMoveTo(safePoint.x,safePoint.y).delay(30);
        }
    }

    @Override
    protected void doExecute() throws Exception {
        mouseAloofWindow(baseFlagAnalysis.windowArea == null? baseFlagAnalysis.getOriginBounds(): baseFlagAnalysis.windowArea);
        List<Point> locations = baseFlagAnalysis.getLocations();
        map.put("points", locations);
        close();
    }
}
